package ext.tianma.part.forms;

import org.apache.commons.lang.StringUtils;

import ext.tianma.part.util.ClassificationUtil;
import wt.fc.WTObject;
import wt.part.Source;
import wt.part.WTPart;
import wt.type.ClientTypedUtility;
import wt.util.WTException;

public class TMTypeUtil {
	/**
	 * 判断零部件类型
	 * @param part
	 * @return
	 */
	public static boolean isRFQPart(WTPart part) {
		return isATypeObject( part , ".RFQPart" ) ;
	}
	/**
	 * 判断零部件类型是否为成品
	 * 
	 * @param part
	 * @return
	 */
	public static boolean isFinishedGood(WTPart part) {
		return isATypeObject( part , ".FinishedGood" ) ;
	}

	/**
	 * 判断零部件类型是否为半成品
	 * 
	 * @param part
	 * @return
	 */
	public static boolean isSemiFinishedGood(WTPart part) {
		
		return isATypeObject( part , ".SemiFinishedGood" ) ;
	}
	
	/**
	 * 判断零部件类型是否为C4类型的半成品
	 * 
	 * @param part
	 * @return
	 */
	public static boolean isC4(WTPart part) {
		
		return isATypeObject( part , ".C4" ) ;
	}

	/**
	 * 判断零部件类型是否为CF类型的半成品
	 * 
	 * @param part
	 * @return
	 */
	public static boolean isCF(WTPart part) {
		
		return isATypeObject( part , ".CF" ) ;
	}
	
	/**
	 * 判断零部件类型是否为二级物料
	 * 
	 * @param part
	 * @return
	 */
	public static boolean isTwoLevelMaterial(WTPart part) {
		
		String classificationPath = ClassificationUtil.queryClassificationPath(part);
		if(StringUtils.isNotBlank(classificationPath) && classificationPath.contains("二级物料")){
			return true;
		}
		return false;
	}
	
	/**
	 * 判断零部件类型是否为BLU类型的二级物料
	 * 
	 * @param part
	 * @return
	 */
	public static boolean isBLU(WTPart part) {
		
		return isATypeObject( part , ".BLU" ) ;
	}
	
	/**
	 * 判断零部件类型是否为FPC_PCB类型的二级物料
	 * 
	 * @param part
	 * @return
	 */
	public static boolean isFPC_PCB(WTPart part) {
		
		return isATypeObject( part , ".FPC_PCB" ) ;
	}
	
	/**
	 * 判断零部件类型是否为原材料
	 * 
	 * @param part
	 * @return
	 */
	public static boolean isRawMaterial(WTPart part) {
		
		return isATypeObject( part , ".RawMaterial" ) ;
	}
	
	/**
	 * 判断零部件类型是否为原材料2
	 * 
	 * @param part
	 * @return
	 */
	public static boolean isRawMaterial2(WTPart part) {
		String typename="wt.part.WTPart|cn.tianma.RawMaterial";
		boolean isATypeObject = false ;
		
		if( typename == null || typename.trim().equals("")){
			return isATypeObject ;
		}
		
		String internalName = getSoftTypeInternal( part ) ;
		System.out.println("internalName2===>>>"+internalName+"============"+typename);
		
		if(internalName != null && internalName.equals( typename )){
			isATypeObject = true ;
		}
		
		return isATypeObject ;
	}
	
	/**
	 * 判断零部件类型是否为偏光片
	 * 
	 * @param part
	 * @return
	 */
	public static boolean isPolarizer(WTPart part) {
		
		return isATypeObject( part , ".Polarizer" ) ;
	}
	
	/**
	 * 判断零部件类型是否为虚拟偏光片
	 * 
	 * @param part
	 * @return
	 */
	public static boolean isPhantomPolarizer(WTPart part) {
		
		return isATypeObject( part , ".PhantomPolarizer" ) ;
	}
	
	/**
	 * 判断零部件类型是否为下偏光片
	 * 
	 * @param part
	 * @return
	 */
	public static boolean isBotPolarizer(WTPart part) {
		
		return isATypeObject( part , ".BotPolarizer" ) ;
	}
	
	/**
	 * 判断零部件类型是否为上偏光片
	 * 
	 * @param part
	 * @return
	 */
	public static boolean isTopPolarizer(WTPart part) {
		
		return isATypeObject( part , ".TopPolarizer" ) ;
	}
	/**
	 * 是否为编号以POL开头的零部件（其实，本质也是原材料）
	 * 
	 * @param part
	 * @return
	 */
	public static boolean isPOLRawMaterial( WTPart part ){
		boolean isPOLRawMaterial = false ;
		
		if( part != null ){
			
			String partNumber = part.getNumber() ;
			
			if(partNumber != null && partNumber.startsWith("POL")){
				isPOLRawMaterial = true ;
			}
		}
		
		return isPOLRawMaterial ;
	}
	
	/**
	 * 判断是否为编号以168开头的虚拟件（其实，本质也是原材料）
	 * 
	 * @param part
	 * @return
	 */
	public static boolean is168VirtualPart( WTPart part ){
		boolean is168VirtualPart = false ;
		
		if( part != null ){
			String partNumber = part.getNumber() ;
			
			if(partNumber != null && partNumber.startsWith("168")){
				
				//判断是否为虚拟件
				if( isVirtualPart( part ) ){
					is168VirtualPart = true ;
				}
			}
		}
		
		return is168VirtualPart ;
	}
	
	private static boolean isVirtualPart( WTPart part ){
		boolean isVirtualPart = false ;
		
		Source source = part.getSource();		
		if( source != null ){			
			String virtualPart = source.getStringValue();
			if( virtualPart.endsWith( ".PHANTOM" ) ){
				isVirtualPart = true ;
			}			
		}
		
		return isVirtualPart ;
	}
	
	/**
	 * 判断obj对象是否为指定类型的对象
	 * 
	 * @param obj
	 * @param typename
	 * @return
	 */
	public static boolean isATypeObject(WTObject obj , String typename) {
		boolean isATypeObject = false ;
		
		if( typename == null || typename.trim().equals("")){
			return isATypeObject ;
		}
		
		String internalName = getSoftTypeInternal( obj ) ;
		System.out.println("internalName===>>>"+internalName+"============"+typename);
		
		if(internalName != null && internalName.contains( typename )){
			isATypeObject = true ;
		}
		
		return isATypeObject ;
	}
	
	/**
	 * 获取对象软类型的内部值
	 * 
	 * @param obj
	 * @return
	 */
	public static String getSoftTypeInternal( WTObject obj) {
		String internalName = "";
		
		if(obj!=null) {
			try {
				//获取完整的内部值
				internalName=ClientTypedUtility.getTypeIdentifier(obj).getTypename() ;
				System.out.println("internalName=========>>>>"+internalName);
			} catch(WTException e) {
				e.printStackTrace() ;
			}
		}
		
		return internalName; 
	}
	
	/**
	 * 判断零部件类型是否为AVL材料
	 * 
	 * @param part
	 * @return
	 */
	public static boolean isAVLMaterial(WTPart part) {
		
		return isATypeObject( part , ".AVLMaterial" ) ;
	}
}
